Telegram Group & Telegram Channel
🐍 Задача на Python — Топ‑3 самых популярных товаров

Допустим, у нас есть список покупок пользователей:


orders = [
{"user": "alice", "items": ["apple", "banana", "apple"]},
{"user": "bob", "items": ["banana", "orange"]},
{"user": "carol", "items": ["banana", "apple", "orange", "banana"]},
{"user": "dave", "items": ["apple"]},
]


🎯 Задание:
Найти 3 самых популярных товара по количеству покупок (не по числу пользователей, а по общему количеству упоминаний).

Ожидаемый результат:

[('banana', 4), ('apple', 4), ('orange', 2)]

💡 Решение:




from collections import Counter

# Собираем все товары в один список
all_items = []
for order in orders:
all_items.extend(order["items"])

# Считаем количество каждого товара
item_counts = Counter(all_items)

# Получаем топ-3 самых популярных
top_3 = item_counts.most_common(3)

print(top_3)


📌 Что тренирует задача:

• Работа со словарями и списками
• Использование Counter из модуля collections
• Умение работать с вложенными структурами
• Сортировка по частоте с помощью most_common()

#python #задача #кодинг #алгоритмы #учимпитон #collections



tg-me.com/pro_python_code/1841
Create:
Last Update:

🐍 Задача на Python — Топ‑3 самых популярных товаров

Допустим, у нас есть список покупок пользователей:


orders = [
{"user": "alice", "items": ["apple", "banana", "apple"]},
{"user": "bob", "items": ["banana", "orange"]},
{"user": "carol", "items": ["banana", "apple", "orange", "banana"]},
{"user": "dave", "items": ["apple"]},
]


🎯 Задание:
Найти 3 самых популярных товара по количеству покупок (не по числу пользователей, а по общему количеству упоминаний).

Ожидаемый результат:

[('banana', 4), ('apple', 4), ('orange', 2)]

💡 Решение:




from collections import Counter

# Собираем все товары в один список
all_items = []
for order in orders:
all_items.extend(order["items"])

# Считаем количество каждого товара
item_counts = Counter(all_items)

# Получаем топ-3 самых популярных
top_3 = item_counts.most_common(3)

print(top_3)


📌 Что тренирует задача:

• Работа со словарями и списками
• Использование Counter из модуля collections
• Умение работать с вложенными структурами
• Сортировка по частоте с помощью most_common()

#python #задача #кодинг #алгоритмы #учимпитон #collections

BY Python RU


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/pro_python_code/1841

View MORE
Open in Telegram


Python RU Telegram | DID YOU KNOW?

Date: |

In many cases, the content resembled that of the marketplaces found on the dark web, a group of hidden websites that are popular among hackers and accessed using specific anonymising software.“We have recently been witnessing a 100 per cent-plus rise in Telegram usage by cybercriminals,” said Tal Samra, cyber threat analyst at Cyberint.The rise in nefarious activity comes as users flocked to the encrypted chat app earlier this year after changes to the privacy policy of Facebook-owned rival WhatsApp prompted many to seek out alternatives.

Telegram is riding high, adding tens of million of users this year. Now the bill is coming due.Telegram is one of the few significant social-media challengers to Facebook Inc., FB -1.90% on a trajectory toward one billion users active each month by the end of 2022, up from roughly 550 million today.

Python RU from pl


Telegram Python RU
FROM USA